package com.ciwong.epaper.modules.reciteWords.ui;

import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.android.volley.TimeoutError;
import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.msg.util.MsgDateFormat;
import com.ciwong.epaper.modules.reciteWords.dao.WordDao;
import com.ciwong.epaper.modules.reciteWords.ui.fragment.MyTimeWheelViewFrament;
import com.ciwong.epaper.modules.reciteWords.util.WordJumpManager;
import com.ciwong.epaper.util.BaseExtCallBack;
import com.ciwong.epaper.util.DialogUtil;
import com.ciwong.epaper.util.IntentFlag;
import com.ciwong.epaper.util.IntentValue;
import com.ciwong.libs.utils.NetworkUtils;
import com.ciwong.mobilelib.i.ViewOnClickListener;
import com.ciwong.mobilelib.ui.BaseActivity;
import com.ciwong.mobilelib.utils.ToastUtil;

public class ChangeWordPlanActivity extends BaseActivity {
	private FrameLayout mFlContainer;
	private TextView    mTvCompleteTime,mTvWordCount,mTvBookName;
	private Button      mBtnSure;
	private int mWordSum,mCompleteCount,mDayCount,mBookId;
	private String mPlanName;
	private int mDays;
	@Override
	protected int setView() {
		return R.layout.activity_change_word_plan;
	}

	@Override
	protected void findViews() {
		mFlContainer = findViewById(R.id.fl_container);
		mTvCompleteTime = findViewById(R.id.tv_complete_time);
		mTvBookName = findViewById(R.id.tv_book_name);
		mTvWordCount = findViewById(R.id.tv_word_count);
		mBtnSure = findViewById(R.id.btn_sure);
	}

	@Override
	protected void initEvent() {
		mBtnSure.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if(!NetworkUtils.isOnline()){
					ToastUtil.INSTANCE.toastCenterNoNetError();
					return;
				}
				int  userId = (int) EApplication.getInstance().getUserInfoBase().getUserId();
				WordDao.saveAndGetWordPlan(userId, mBookId, mPlanName,  mDayCount, mDays, new BaseExtCallBack(ChangeWordPlanActivity.this) {
					@Override
					public void failed(int errorCode, Object data) {
						hideCricleProgress();
						super.failed(errorCode, data);
						ToastUtil.INSTANCE.toastCenterErrorWithErrorCode(R.string.server_error,errorCode);
					}

					@Override
					public void failed(Object data) {
						hideCricleProgress();
						super.failed(data);
						if (!NetworkUtils.isOnline()) {
							ToastUtil.INSTANCE.toastCenterNoNetError();
						} else if (data instanceof TimeoutError) {
							ToastUtil.INSTANCE.toastCenterError("连接超时，请稍后重试");
						}
					}

					@Override
					public void success(Object data) {
						super.success(data);
						hideCricleProgress();
						WordJumpManager.jumpToWordPlanActivity(R.string.go_back, IntentValue.JumpWordPlanType.FROM_CHANGE_PLAN,null,ChangeWordPlanActivity.this);
						finish();
					}
				});
			}
		});
		setRightBtnListener(new ViewOnClickListener() {
			@Override
			public void avertRepeatOnClick(View v) {
				if(!NetworkUtils.isOnline()){
					ToastUtil.INSTANCE.toastCenterNoNetError();
					return;
				}
				String tips = getResources().getString(R.string.change_plan_tips);
				DialogUtil.showConfirmDialog(ChangeWordPlanActivity.this, tips, "确定", "取消", new DialogUtil.OnConfirmClickListener() {
					@Override
					public void onConfirmClick() {
						WordJumpManager.jumpToSelectVacabularyActivity(R.string.go_back, ChangeWordPlanActivity.this,mBookId, IntentValue.JumpSelectVocabularyType.FROM_CHANGE_PLAN);

					}
				});
			}
		});
	}

	@Override
	protected void init() {
		setTitleText("修改计划");
		setRightBtnText("切换教材");
		Intent intent = getIntent();
		if(intent!=null){
			mWordSum = intent.getIntExtra(IntentFlag.INTENT_FLAG_TOTAL_NUM,0);
			mPlanName=	intent.getStringExtra(IntentFlag.INTENT_FLAG_PLAN_NAME);
			mCompleteCount =intent.getIntExtra(IntentFlag.INTENT_FLAG_COMPLETE_COUNT,0);
			mDayCount =intent.getIntExtra(IntentFlag.INTENT_FLAG_SCHEDULE_DAY_COUNT,0);
			mBookId =intent.getIntExtra(IntentFlag.INTENT_FLAG_BOOKID,0);
		}
	}

	@Override
	protected void loadData() {
		mTvBookName.setText(mPlanName);
		mTvWordCount.setText(mWordSum+"个单词");

		Bundle bundle = new Bundle();
		bundle.putInt(IntentFlag.INTENT_FLAG_TOTAL_NUM, mWordSum-mCompleteCount);
		bundle.putInt(IntentFlag.INTENT_FLAG_SCHEDULE_DAY_COUNT, mDayCount);
		MyTimeWheelViewFrament myTimeWheelViewFrament = MyTimeWheelViewFrament.newInstance(bundle);
		FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
		fragmentTransaction.add(R.id.fl_container, myTimeWheelViewFrament);
		fragmentTransaction.commit();
		myTimeWheelViewFrament.setOnDaySelectedListener(new MyTimeWheelViewFrament.onDaySelectedListener() {


			@Override
			public void onDaySeleted(int days, int dayWordNumber) {
				mDayCount = dayWordNumber;
				mDays = days;
				String time = MsgDateFormat.formatDate2(days);
				mTvCompleteTime.setText(time);
			}
		});

	}
}
